<?php

require_once './db_connection.php';

class sign extends db_connection{

	public $license_key;
	public $course_id;
	public $data;
	public $student_id;
	public $list;

	function create_list($list){
		$n = count($list);
		$i = 0;
		while($i<$n){
			foreach($list[$i] as $k=>$v){
				$q .= $i."-".$k."=>".$v.";\r\n";
			}
			$q .= "\r\n";
			file_put_contents("test.txt",$q);
			$i++;	
		}
	}

	function read_list(){
		$s = file_get_contents('test.txt');
		echo $s;
		preg_match_all('/data_status(.*?)>/is',$s,$n);
		$n = count($n);
		$i = 0;
		//while(){}

	}

	function get_key($key){
		$this->license_key = $key;
		return $this->get_course();
	}

	function get_course(){
		$sql = "SELECT course_info.c_id FROM course_info WHERE course_info.t_id = '1'";
		$s = $this->db->query($sql);
		return $this->course_id = $s->fetchColumn();
		//return $this->get_list();
	}

	function get_list(){
		$this->get_student_id();
		if($this->student_id==''){
			echo $this->student_id;
			echo "无该学生信息";
			return;
		}
		//echo $this->student_id;
		$sql = "SELECT sign_info.*,student_info.c_no FROM sign_info,student_info WHERE c_id =  '$this->course_id' AND student_info.s_id = '$this->student_id' AND sign_info.s_id = '$this->student_id'";
		$s = $this->db->query($sql);
		$this->list = $s->fetchAll(PDO::FETCH_OBJ);
		if(!empty($this->list)){
			echo "TRUE";
		}else{
			echo "该生没有选改课程";
			return;
		}
		
		//$this->create_list($list);
		//return $list;
	}

	function get_student_id(){
		$no = $this->data['no'];
		$sql = "SELECT student_info.s_id FROM student_info WHERE student_info.c_no = '$no' ";
		//echo $sql;
		$q = $this->db->query($sql);
		$this->student_id = $q->fetchColumn();
	}

	function start_sign($post){
		$this->data = $post;
		$this->card_in();
	}

	function card_in(){
		$name = $this->data['name'];
		$sex = $this->data['sex'];
		$no = $this->data['no'];
		$this->course_id = $this->data['course_id'];
		$sql = "SELECT count(*) FROM `card_info` WHERE c_no = '$no'";
		$s = $this->db->query($sql);
		if(!$s->fetchColumn()){
			$sql = "INSERT INTO `card_info`(`name`, `sex`, `c_no`) VALUES ('$name','$sex','$no')";
			if($this->db->exec($sql)){
			}else{
				echo "录入失败";
			}
		}
		$this->get_list();
		//var_dump($this->list);
		//$this->info_check();
	}

	function info_check(){
		
	}
}
$sign = new sign();
?>
